package com.lifu.yygh.user.api;

import com.lifu.yygh.common.result.Result;
import com.lifu.yygh.common.utils.AuthContextHolder;
import com.lifu.yygh.model.user.Patient;
import com.lifu.yygh.user.service.PatientService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Description
 * @Author lifu
 * @Date 2021/5/22 20:42
 */
@Api(tags = "就诊人信息接口")
@RestController
@RequestMapping(value = "/api/user/patient")
public class PatientApiController {
    @Autowired
    private PatientService patientService;

    //获取就诊人的列表
    @ApiOperation(value = "获取就诊人的列表")
    @GetMapping(value = "/auth/findAll")
    public Result findAll(HttpServletRequest httpServletRequest){
        Long userId = AuthContextHolder.getUserId(httpServletRequest);
        List<Patient> list = patientService.findAllByUserId(userId);
        return Result.ok(list);
    }
    //添加就诊人
    @ApiOperation(value = "添加就诊人")
    @PostMapping(value = "/auth/save")
    public Result savePatient(@RequestBody Patient patient,HttpServletRequest httpServletRequest){
        Long userId = AuthContextHolder.getUserId(httpServletRequest);
        patient.setUserId(userId);
        patientService.save(patient);
        return Result.ok();
    }
    //根据就诊人id获取就诊人信息
    @ApiOperation(value = "根据就诊人id获取就诊人信息")
    @GetMapping(value = "/auth/get/{id}")
    public Result getPatient(@PathVariable Long id){
        Patient patient = patientService.getPatientById(id);
        return Result.ok(patient);
    }
    //修改就诊人信息
    @ApiOperation(value = "修改就诊人信息")
    @PutMapping(value = "/auth/update")
    public Result updatePatient(@RequestBody Patient patient){
        patientService.updateById(patient);
        return Result.ok();
    }
    //删除就诊人信息
    @ApiOperation(value = "用户手机号登录接口")
    @DeleteMapping(value = "/auth/remove/{id}")
    public Result removePatient(@PathVariable Long id){
        patientService.removeById(id);
        return Result.ok();
    }
    //内部接口
    @ApiOperation(value = "获取就诊人")
    @GetMapping("/inner/get/{id}")
    public Patient getPatientOrder(
            @ApiParam(name = "id", value = "就诊人id", required = true)
            @PathVariable("id") Long id) {
        Patient patient = patientService.getPatientById(id);
        return patient;
    }
}
